package com.doyens.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.gmall.common.result.Result;
import com.doyens.gmall.model.product.BaseSaleAttr;
import com.doyens.gmall.model.product.SpuImage;
import com.doyens.gmall.model.product.SpuInfo;
import com.doyens.gmall.model.product.SpuSaleAttr;
import com.doyens.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "spu商品信息接口")
@RestController
@RequestMapping("admin/product")
public class SpuManageController {
    @Autowired
    private ManageService manageService;

    //1/10?category3Id=61
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable("page") Long page, @PathVariable("limit") long limit,
                                 @RequestParam(required = true) Long category3Id) {
        Page<SpuInfo> pageParam = new Page<>(page, limit);
        SpuInfo spuInfo = new SpuInfo();
        spuInfo.setCategory3Id(category3Id);
        IPage<SpuInfo> iPage = manageService.getSpuInfoPage(pageParam, spuInfo);
        return Result.ok(iPage);
    }

    //http://api.gmall.com/admin/product/baseSaleAttrList  get
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrs = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrs);
    }

    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){
       List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
       return Result.ok(spuSaleAttrList);
    }

}
